package com.allwees.bs.module.fms.service.impl;

import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.fms.dao.SupplierSettleStatementDao;
import com.allwees.bs.module.fms.entity.SupplierSettleStatement;
import com.allwees.bs.module.fms.req.DoSettleReq;
import com.allwees.bs.module.fms.service.MerchantAccountLogService;
import com.allwees.bs.module.fms.service.MerchantAccountService;
import com.allwees.bs.module.fms.service.SupplierSettleStatementService;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@Service
public class SupplierSettleStatementServiceImpl extends BoServiceImpl<SupplierSettleStatementDao, SupplierSettleStatement>
		implements SupplierSettleStatementService {
	@Autowired
	private GeneralOmsOrderItemService _generalOmsOrderItemService;

	@Autowired
	private MerchantAccountService merchantAccountService;

	@Autowired
	private MerchantAccountLogService merchantAccountLogService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void doSettle(OmsOrderItem omsOrderItem,DoSettleReq req, boolean exceedSettleDate){
		//OmsOrderItem orderItem = _generalOmsOrderItemService.doSettled(omsOrderItem,req.getBranchUuid(),exceedSettleDate);
		//SupplierSettleStatement settleStatement = _createSettleStatement(req.getBranchUuid(), orderItem);

		//MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.SUPPLIER, settleStatement.getSupplierUuid());
		//Assert.notNull(account, "商户[" + settleStatement.getSupplierUuid() + "]结算账户为空");
		//account.doSettle(settleStatement);
		//merchantAccountService.updateByUuid(account);
		//_logSettle(account, settleStatement);
		//return settleStatement;
	}

	/*private SupplierSettleStatement _createSettleStatement(String supplierUuid, OmsOrderItem orderItem) {
		SupplierSettleStatement settleStatement = new SupplierSettleStatement();
		settleStatement.setSupplierUuid(supplierUuid);
		settleStatement.setOrderItemUuid(orderItem.getUuid());
		settleStatement.setOrderNo(orderItem.getOrderNo());
		settleStatement.setOrderUuid(orderItem.getOrderUuid());
		settleStatement.setNo(FmsNoUtil.genSettleStatementNo(EBranchType.SUPPLIER, supplierUuid));
		settleStatement.setCurrency(CurrencyCode.USD.name());
		BigDecimal amountTotal = BigDecimal.ZERO;

		BigDecimal amount = MoneyUtil.mul(orderItem.getSupplyPrice(), BigDecimal.valueOf(orderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, amount);

		settleStatement.setAmount(amountTotal);
		save(settleStatement);
		return settleStatement;
	}

	private void _logSettle(MerchantAccount account, SupplierSettleStatement settleStatement) {
		MerchantAccountLog accountLog = new MerchantAccountLog();
		accountLog.setBranchUuid(account.getBranchUuid())
				.setBalanceType(EBalanceType.INCOME)
				.setBsType(EAccountLogBsType.ORDER_SETTLED)
				.setNo(FmsNoUtil.genAccountLogNo(account.getBranchUuid(), EBalanceType.INCOME, EAccountLogBsType.ORDER_SETTLED))
				.setAccountUuid(account.getUuid())
				.setAccountNo(account.getAccountNo())
				.setTakeAmount(settleStatement.getAmount())
				.setCurrency(settleStatement.getCurrency())
				.setFee(BigDecimal.ZERO)
				.setBalance(account.getBalance())
				.setBalanceCurrency(account.getCurrency());
		merchantAccountLogService.create(accountLog);
	}*/
}
